#include "signup.h"
#include "ui_signup.h"
#include "login.h"
#include <QGraphicsDropShadowEffect>
#include "sersocket.h"
#include "localdb.h"
#include <QWidget>


signup::signup(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::signup)
{
    this->setWindowTitle("IM_software sign_up");
    this->setWindowIcon(QIcon(":/new/prefix1/images/icons_head.png"));

    m_socket = new SerSocket(this);
    ui->setupUi(this);
    QPixmap *pix = new QPixmap(":/new/prefix1/images/girl1.png");
    QSize sz = ui->label_image->size();
    ui->label_image->setPixmap(pix->scaled(sz));

    connect(this,&signup::Online,m_socket,&SerSocket::ClientSendingText);
    connect(m_socket, &SerSocket::Signal,this, &signup::signupResult);
}


signup::~signup()
{
    delete ui;
}

//返回登录按钮
void signup::on_btn_return_clicked()
{

    login *w = new login;
    w->show();
    this->close();
}

//注册按钮
void signup::on_pushButton_2_clicked()
{
    sqlite_Init();
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_passwd->text();
    QString surepass = ui->lineEdit_surepasswd->text();

    if(username.isEmpty()){
        QMessageBox::information(this,"注册认证","用户名不能为空");
        return;
    }
    //判断密码是否一致
    if(password == surepass)
    {
        // 上传到服务器
        QJsonObject* sign_info = new QJsonObject;

        sign_info->insert("name", ui->lineEdit_username->text());
        sign_info->insert("password", ui->lineEdit_passwd->text());
        QJsonDocument jsonDocument;
        jsonDocument.setObject(*sign_info);
        emit Online(jsonDocument.toJson(), 0x04);

        QString sql=QString("insert into user values('%1','%2');")
                          .arg(username).arg(password);

    }else{
        QMessageBox::information(this,"注册认证","两次密码输入不一致");
    }
}

void signup::signupResult(const QString rlt){
    QMessageBox::information(this,"注册认证",rlt);
}
